package com.nes.rabbitmq.local;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * EndPoint类型的队列
 */
public abstract class EndPoint{
     
    protected Channel channel;
    protected Connection connection;
    protected String queueName;
     
    public EndPoint(String queueName) throws IOException, TimeoutException {
         this.queueName = queueName;
         
         //创建一个连接工厂
         ConnectionFactory factory = new ConnectionFactory();
         
         //设置rabbitmq　主机　端口　用户＆密码
         factory.setHost("127.0.0.1");
         factory.setPort(5672);
         factory.setUsername("admin");
         factory.setPassword("admin");
         
         //创建一个连接
         connection = factory.newConnection();
         
         //creating a channel
         channel = connection.createChannel();

         //declaring a queue for this channel. If queue does not exist,
         //it will be created on the server.
         channel.queueDeclare(queueName, false, false, false, null);
    }
     
     
    /**
     * 关闭channel和connection。并非必须，因为隐含是自动调用的。
     * @throws IOException
     */
     public void close() throws IOException, TimeoutException {
         this.channel.close();
         this.connection.close();
     }
}